<template>
  <div>
    <div>
      <el-form :inline="true" :model="form">
        <el-button-group>
          <!--<el-button type="primary" icon="edit" @click="editHandler"/>-->
          <el-button type="primary" @click="editHandler">审核</el-button>
          <el-input v-model="form.fileName" clearable placeholder="企业名称" style="width:200px;"/>
          <el-select v-model="form.region" placeholder="是否有效">
            <el-option label="全部" value=""></el-option>
            <el-option label="待审核" value="0"></el-option>
            <el-option label="审核通过" value="1"></el-option>
            <el-option label="审核未通过" value="2"></el-option>
          </el-select>
          <el-form-item>
            <el-button type="primary" @click="_querybankByFileName">搜索</el-button>
          </el-form-item>
        </el-button-group>
      </el-form>
    </div>

    <el-table :data="getSysFiles" height="300" border :stripe="true" :highlight-current-row="true"
              @current-change="tableRowChangeClickHandler"
              style="width: 100%">
      <el-table-column prop="profileId" label="企业编号" min-width="20%"/>
      <el-table-column prop="profileName" label="企业名称" min-width="20%"/>
      <el-table-column prop="ubOrganBankOwner" label="银行账户名称" min-width="20%"/>
      <el-table-column prop="ubBankName" label="开户银行" min-width="20%"/>
      <el-table-column prop="ubOrganBankNo" label="银行账号" min-width="20%"/>
      <el-table-column prop="ubBankBranchName" label="支行名称" min-width="20%"/>
      <el-table-column prop="auditStatus" label="状态" min-width="20%" :formatter="formatterStateHandler"/>
    </el-table>

    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="getSysFileCurrentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="getFilesCount">
      </el-pagination>
    </div>

    <upload-form ref="uploadForm" :mode="formMode" :param="currentRecord"/>

    <button v-clipboard="copyData" @success="copySucHandler" @error="copyErrorHandler"
            ref="btnCopy" style="display:none">copy
    </button>


  </div>
</template>

<script type="text/babel">
  import { mapActions } from 'vuex'

  import bankManagementStore from './bll/bankManagementStore'
  import store from '../../store'

  (!store.state.bankManagementStore) && store.registerModule('bankManagementStore', bankManagementStore)

  import UploadForm from './uploadForm.vue'

  export default {
    data () {
      return {
        form: {
          fileName: '',
          region: ''
        },
        formMode: null,
        currentRecord: null,
        pageSize: 10,
        pageIndex: 1,
        //form: {},
        copyData: '',
        qrData: '',
        privFileUrlFlag: false
      }
    },
    components: {UploadForm},
    computed: {
      getSysFiles () {
        return this.$store.state.bankManagementStore.files
      },
      getFilesCount () {
        return this.$store.state.bankManagementStore.totalCount
      },
      getSysFileCurrentPage () {
        return this.$store.state.bankManagementStore.currentPage
      }
    },

    mounted () {
      console.log('file upload page mounted.')
      this.initPage()
    },

    methods: {
      ...mapActions(['searchBank', 'queryBank', 'editBank']),

      initPage () {
        this.queryBank()
      },

      openUploadDialogHandler () {
        this.$refs.uploadDialog.open()
      },

      editHandler () {
        if (!this.currentRecord) {
          this.$message.warning('请选择一条记录')
          return
        }

        this.$refs.uploadForm.enable()
        this.formMode = 'EDIT'
      },
      copySucHandler (e) {
        this.$message.info('链接拷贝成功')
        this.privFileUrlFlag = false
      },
      copyErrorHandler (e) {
        console.error('fail to copy url', e)
        let msg = '拷贝失败，请手动复制地址'
        if (this.privFileUrlFlag) {
          msg = '拷贝失败，请再尝试一次'
        }
        this.$message.warning(msg)
      },

      tableRowChangeClickHandler (currentRow, oldCurrentRow) {
        console.log(currentRow)
        this.currentRecord = currentRow
        this.$refs.uploadForm.disable()
      },

      handleSizeChange (val) {
        console.log(`每页 ${val} 条`)
        this.pageSize = val
        var state = this.form.region
        var profileName = this.form.fileName
        this.searchBank({profileName: profileName, state: state, pageIndex: 1, pageSize: val})
      },
      handleCurrentChange (val) {
        console.log(arguments)
        console.log(`current${val}`)
        this.pageIndex = val
        var state = this.form.region
        var profileName = this.form.fileName
        this.searchBank({profileName: profileName, state: state, pageIndex: val, pageSize: this.pageSize})
      },
      formatterHandler (row, column) {
        switch (column.property) {
          case 'state':
            return row.state === 1 ? 'Yes' : 'Discard'
        }
      },
      _querybankByFileName () {
        console.log(this.form.fileName)
        var profileName = this.form.fileName
        var state = this.form.region
        console.log(profileName)
        console.log(state)
        this.searchBank({profileName: profileName, state: state})
//        if (this.form.fileName !== '' || this.form.region !== '') {
//          this.searchOperator({profileName: profileName, state: state})
//          this.searchOperatorCount({profileName: profileName, state: state})
//        } else {
//          this.queryOperator()
//          this.queryOperatorCount()
//        }
      },
      formatterStateHandler (row, column) {
        switch (column.property) {
          case 'auditStatus':
            switch (row.auditStatus) {
              case 0:
                return '待审核'
              case 1:
                return '审核通过'
              case 2:
                return '审核未通过'
            }
            break
        }
        return 'Unkown key'
      }
    }
  }
</script>

<style lang="scss">
  .download-url {
    padding: 5px;
  }

  .qrcode-wrapper {
    margin: auto;
  }
</style>

